﻿Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Runtime.InteropServices
Imports System.Windows.Interop

Partial Public Class PinInputWindow
    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()

        ' 在此點下方插入建立物件所需的程式碼。
    End Sub

    '#Region "AreoEffect"
    '    <StructLayout(LayoutKind.Sequential)>
    '    Public Structure MARGINS
    '        Public Sub New(t As Thickness)
    '            With t
    '                Left = .Left
    '                Right = .Right
    '                Top = .Top
    '                Bottom = .Bottom
    '            End With
    '        End Sub
    '        Public Sub New(margin As Integer)
    '            Left = margin
    '            Right = margin
    '            Top = margin
    '            Bottom = margin
    '        End Sub
    '        Public Left As Integer
    '        Public Right As Integer
    '        Public Top As Integer
    '        Public Bottom As Integer
    '    End Structure

    '    Private Const WM_DWMCOMPOSITIONCHANGED = &H31E

    '    <DllImport("dwmapi.dll", PreserveSig:=False)>
    '    Public Shared Sub DwmExtendFrameIntoClientArea(hWnd As IntPtr, ByRef pMarInset As MARGINS)

    '    End Sub
    '    <DllImport("dwmapi.dll", PreserveSig:=False)>
    '    Public Shared Function DwmIsCompositionEnabled() As Boolean

    '    End Function

    '    Public Function ExtendGlassFrame(win As Window, margin As Thickness) As Boolean
    '        If Not DwmIsCompositionEnabled() Then
    '            Return False
    '        End If
    '        Dim hwnd As IntPtr = New WindowInteropHelper(win).Handle
    '        If hwnd = IntPtr.Zero Then
    '            Return False
    '        End If
    '        win.Background = Brushes.Transparent
    '        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent

    '        Dim mar = New MARGINS(margin)
    '        DwmExtendFrameIntoClientArea(hwnd, mar)
    '        Return True
    '    End Function

    '    Public Function ExtendGlassFrame(win As Window, margin As Integer) As Boolean
    '        If Not DwmIsCompositionEnabled() Then
    '            Return False
    '        End If
    '        Dim hwnd As IntPtr = New WindowInteropHelper(win).Handle
    '        If hwnd = IntPtr.Zero Then
    '            Return False
    '        End If
    '        win.Background = Brushes.Transparent
    '        HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent

    '        Dim mar = New MARGINS(margin)
    '        DwmExtendFrameIntoClientArea(hwnd, mar)
    '        Return True
    '    End Function

    '    Protected Overrides Sub OnSourceInitialized(e As System.EventArgs)
    '        MyBase.OnSourceInitialized(e)
    '        ExtendGlassFrame(Me, -1)
    '        Dim hwnd As IntPtr = New WindowInteropHelper(Me).Handle
    '        HwndSource.FromHwnd(hwnd).AddHook(New HwndSourceHook(AddressOf WndProc))
    '    End Sub

    '    Private Function WndProc(hwnd As IntPtr, msg As Integer, wParam As IntPtr, lParam As IntPtr, ByRef handled As Boolean) As IntPtr
    '        If msg = WM_DWMCOMPOSITIONCHANGED Then
    '            ExtendGlassFrame(Me, -1)
    '            handled = True
    '        End If
    '        Return IntPtr.Zero
    '    End Function
    '#End Region

    Private Shared ReadOnly PINProperty As DependencyProperty =
        DependencyProperty.Register("PIN", GetType(String), GetType(PinInputWindow))

    Public Property PIN As String
        Get
            Return CStr(GetValue(PINProperty))
        End Get
        Set(value As String)
            SetValue(PINProperty, value)
        End Set
    End Property

    Private Sub PinInputWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Me.CommandBindings.Add(New CommandBinding(Commands.ApplyPin, AddressOf Apply_Executed, New CanExecuteRoutedEventHandler(AddressOf Apply_CanExecuted)))
        Me.CommandBindings.Add(New CommandBinding(Commands.CancelEdit, AddressOf Cancel_Executed))
    End Sub

    Private Sub Apply_Executed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.DialogResult = True
    End Sub

    Private Sub Apply_CanExecuted(ByVal sender As Object, ByVal e As CanExecuteRoutedEventArgs)
        If String.IsNullOrEmpty(PIN) Then
            e.CanExecute = False
        Else
            e.CanExecute = True
        End If
    End Sub

    Private Sub Cancel_Executed(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs)
        Me.DialogResult = True
    End Sub

    Private Sub PinInputWindow_MouseLeftButtonDown(sender As Object, e As System.Windows.Input.MouseButtonEventArgs) Handles Me.MouseLeftButtonDown
        Me.DragMove()
    End Sub
End Class
